package engine;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import javax.activation.DataSource;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import engine.dataobjects.MailItem;
import engine.dataobjects.MailItemInstance;
import engine.services.IDataResource;
import engine.services.IMailService;


public class LiveMailService {
	@Test
	//@Ignore
	public void myMailService() {
		try {
			//System.setProperty("user.dir", "C:/Users/Ilanap/Personal/projects/MailPusher/src/main/resources");
			
			String userDir  = System.getProperty("user.dir").replace("////", "/");
			System.setProperty("log4j.configuration","file:/"+userDir+"/src/main/resources/META-INF/log4j.xml");
			System.setProperty("java.net.preferIPv4Stack", "true");
			
			String myappcontext = "C:/Users/IlanaP/Personal/projects/scheduledmailer/standalone_app/src/main/resources/META-INF/ScheduledMailerApplicationContext.xml";
			//String myappcontext = userDir+"/src/main/resources/META-INF/ScheduledMailerApplicationContext.xml";
			ApplicationContext context = new FileSystemXmlApplicationContext(myappcontext);

			//Main main = new Main(new String[] {"-debug"}, "file:**/META-INF/ScheduledMailerApplicationContext.xml", 300L * 1000L);
//			IDataResource configResource = (IDataResource)context.getBean("configResource");
//			configResource.init();
			IMailService service = (IMailService)context.getBean("mailService");
//			List<MailItemInstance> items = configResource.getActiveMailItemInstances();
//			for (MailItemInstance item :items) {
//				
//				System.out.println("Running: " + item.getItem().getName());
//				service.mail(item);
//
//			}
			
			MailItem<engine.dataobjects.xml.MailItem> message = new MailItem();
			message.setName("TestMessage");
			message.setParser("engine.parsers.impl.FSHTMLParser");
			message.setUrl("http://www.foksuk.nl");
			MailItemInstance<Object> instance = new MailItemInstance(message);
			List<String> to = new ArrayList();
			to.add("ilana.paktor@amdocs.com");
			instance.setTo(to);
			service.mail(instance );
			System.out.println("done");
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	
	public static void main(String[] args) {

//	    try {
//	    	System.out.println("Trying to ping server...");
//			String ipAddress = "mail.013net.net";
//		    //InetAddress inet = InetAddress.getByName(ipAddress);
//		//	System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
//
//			Proxy proxy = new Proxy(Type.SOCKS, new InetSocketAddress("genproxy.amdocs.com", 8080));
//			
//	    	System.out.println("Trying to connect to socket...");
//			Socket socket = null;
//			try {
//				socket = new Socket(proxy);
//			    InetSocketAddress socketAddress = new InetSocketAddress(ipAddress, 25);
//			    socket.connect(socketAddress);
//			} finally {            
//			    if (socket != null) try { socket.close(); } catch(IOException e) {
//			    	throw e;
//			    } else if (socket == null) {
//			    	System.err.println("No socket.");
//			    } else {
//				    System.out.println("Socket opened and closed.");			    	
//			    }
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//			return;
//		}


		try {
			System.setProperty("java.net.preferIPv4Stack", "true");
			JavaMailSenderImpl mailer = new JavaMailSenderImpl();
			//mailer.setPort(25);
			mailer.setHost("tlv.amdocs.com");
			SimpleMailMessage simpleMessage = new SimpleMailMessage();
			simpleMessage.setReplyTo("ilanap@amdocs.com");
			simpleMessage.setTo("ilanap@amdocs.com");
			simpleMessage.setFrom("ilanap@amdocs.com");
			simpleMessage.setText("tralala");
//			MimeMessage mm = mailer.createMimeMessage();
//			mm.setRecipient(RecipientType.TO, new InternetAddress("ilanap@amdocs.com"));
//			MimeMessageHelper helper = new MimeMessageHelper(mm, true);
//		
//			File file = new File("C:/Users/IlanaP/Personal/projects/scheduledmailer/engine/src/test/resources/webserver_pages/images_logo_lg.gif");
//			if (!file.exists()) throw new FileNotFoundException();
//
//			helper.addAttachment("images_logo_lg.gif", file );
//			mailer.send(mm );
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
	

}
